/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Global Trust Authority is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

use base64::{engine::general_purpose, Engine as _};
use plugin_manager::PluginError;

use virtcca_verifier::tokens::VccaToken;

#[test]
fn test_vcca_token_new_success() {
    let token_str = "2QGPoRms0VkFjdKERKEBOCSgWQN/WQN8pwpYQNAPJBAS5bQL7bpR3h5lcEgsheCvXCkaS22Mos05ojzljcug4aPrrNrZUOlMo8FAlbmNkK/IFE8lh1r6DbZ1TLUZrMtYQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZrM1ZAiYwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDC7lheuOGfZ/3yj4qHwcD2L3llaS/bzBT3Mpo7kdwBdnqJJ45TidzlagjRJyfBopTTQT/7lvDhGtEEnp91gNrfirJXgCfobtjS/uwQ/wd+yezAQv4pQJC7TtY1rvpxgiG9i5yto1ZjcF64KJYA2ehnqUTRiizNziOfkY6jf7jL04X1Yo29ZmjPVOx0b9AmvczJzALB2Dn9tZrgS4H/q7CRGZBvkeBK3ZU+ok9y0mMpmPqNUTARu/AIx1LVgCH4QPCwgetEwXCWx+UilEBbBZ7mfyrzvjW9kWM6l7vTzyYRsMdTQnqs0Xcwdv7PK4WlQ/lMs/WeNcEfUBu+sWE2zo16kBojNHypOun1sfg2FyLLmvpJOFznG4qUurJN5Lik9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qnKgFGDPAqTKcLf9sA9hp4sFQHDEYcYRnUwS2WNJqK7TDT0hAoD9FjvFZWItYAM8fMTEwd8bRrl9UO+nwANgMyZyuf4aeMUj7lF44osVT/fUcMmD60cP9+7SoQBFnpiFmzDtPsk9C3oF7Uv6lOui9lrud2+ryQSUsM39+njpXonFKfr1BO";
    let token_bytes = general_purpose::STANDARD.decode(token_str).expect("Failed to decode base64 token");
    let result = VccaToken::new(&token_bytes);
    assert!(result.is_ok());
}

#[test]
fn test_vcca_token_new_invalid_token() {
    let invalid_token_bytes = b"invalid_token";
    let result = VccaToken::new(invalid_token_bytes);
    assert!(result.is_err());
}

#[test]
fn test_vcca_token_verify_cvm_token_success() {
    let token_str = "2QGPoRms0VkFjdKERKEBOCSgWQN/WQN8pwpYQNAPJBAS5bQL7bpR3h5lcEgsheCvXCkaS22Mos05ojzljcug4aPrrNrZUOlMo8FAlbmNkK/IFE8lh1r6DbZ1TLUZrMtYQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZrM1ZAiYwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDC7lheuOGfZ/3yj4qHwcD2L3llaS/bzBT3Mpo7kdwBdnqJJ45TidzlagjRJyfBopTTQT/7lvDhGtEEnp91gNrfirJXgCfobtjS/uwQ/wd+yezAQv4pQJC7TtY1rvpxgiG9i5yto1ZjcF64KJYA2ehnqUTRiizNziOfkY6jf7jL04X1Yo29ZmjPVOx0b9AmvczJzALB2Dn9tZrgS4H/q7CRGZBvkeBK3ZU+ok9y0mMpmPqNUTARu/AIx1LVgCH4QPCwgetEwXCWx+UilEBbBZ7mfyrzvjW9kWM6l7vTzyYRsMdTQnqs0Xcwdv7PK4WlQ/lMs/WeNcEfUBu+sWE2zo16kBojNHypOun1sfg2FyLLmvpJOFznG4qUurJN5Lik9zYmRrVAAvwFbShkgk9zsCFcV/FSUf65Vd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nUwS2WNJqK7TDT0hAoD9FjvFZWItYAM8fMTEwd8bRrl9UO+nwANgMyZyuf4aeMUj7lF44osVT/fUcMmD60cP9+7SoQBFnpiFmzDtPsk9C3oF7Uv6lOui9lrud2+ryQSUsM39+njpXonFKfr1BO";
    let token_bytes = general_purpose::STANDARD.decode(token_str).expect("Failed to decode base64 token");
    let mut vcca_token = VccaToken::new(&token_bytes).unwrap();
    let nonce = vcca_token.tokens.cvm_token.challenge;
    assert!(vcca_token.verify_cvm_token(Some(&nonce)).is_ok());
}

#[test]
fn test_vcca_token_verify_cvm_token_nonce_mismatch() {
    let token_str = "2QGPoRms0VkFjdKERKEBOCSgWQN/WQN8pwpYQNAPJBAS5bQL7bpR3h5lcEgsheCvXCkaS22Mos05ojzljcug4aPrrNrZUOlMo8FAlbmNkK/IFE8lh1r6DbZ1TLUZrMtYQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZrM1ZAiYwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDC7lheuOGfZ/3yj4qHwcD2L3llaS/bzBT3Mpo7kdwBdnqJJ45TidzlagjRJyfBopTTQT/7lvDhGtEEnp91gNrfirJXgCfobtjS/uwQ/wd+yezAQv4pQJC7TtY1rvpxgiG9i5yto1ZjcF64KJYA2ehnqUTRiizNziOfkY6jf7jL04X1Yo29ZmjPVOx0b9AmvczJzALB2Dn9tZrgS4H/q7CRGZBvkeBK3ZU+ok9y0mMpmPqNUTARu/AIx1LVgCH4QPCwgetEwXCWx+UilEBbBZ7mfyrzvjW9kWM6l7vTzyYRsMdTQnqs0Xcwdv7PK4WlQ/lMs/WeNcEfUBu+sWE2zo16kBojNHypOun1sfg2FyLLmvpJOFznG4qUurJN5Lik9zYmRrVAAvwFbShkgk9zsCFcV/FSUf65Vd1BagejWgckj54po1dNQgeDLKJ1IuM8sNCcGhaqRC4QbIVORxV9uMU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ANgMyZyuf4aeMUj7lF44osVT/fUcMmD60cP9+7SoQBFnpiFmzDtPsk9C3oF7Uv6lOui9lrud2+ryQSUsM39+njpXonFKfr1BO";
    let token_bytes = general_purpose::STANDARD.decode(token_str).expect("Failed to decode base64 token");
    let mut vcca_token = VccaToken::new(&token_bytes).unwrap();
    let wrong_nonce = b"wrong_nonce";
    let result = vcca_token.verify_cvm_token(Some(wrong_nonce));
    assert!(result.is_err());
    if let Err(PluginError::InputError(msg)) = result {
        assert!(msg.contains("Cvm token challenge does not match nonce"));
    } else {
        panic!("Expected InputError for nonce mismatch");
    }
}

#[test]
fn test_vcca_token_to_json_value() {
    let token_str = "2QGPoRms0VkFjdKERKEBOCSgWQN/WQN8pwpYQNAPJBAS5bQL7bpR3h5lcEgsheCvXCkaS22Mos05ojzljcug4aPrrNrZUOlMo8FAlbmNkK/IFE8lh1r6DbZ1TLUZrMtYQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZrM1ZAiYwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQDC7lheuOGfZ/3yj4qHwcD2L3llaS/bzBT3Mpo7kdwBdnqJJ45TidzlagjRJyfBopTTQT/7lvDhGtEEnp91gNrfirJXgCfobtjS/uwQ/wd+yezAQv4pQJC7TtY1rvpxgiG9i5yto1ZjcF64KJYA2ehnqUTRiizNziOfkY6jf7jL04X1Yo29ZmjPVOx0b9AmvczJzALB2Dn9tZrgS4H/q7CRGZBvkeBK3ZU+ok9y0mMpmPqNUTARu/AIx1LVgCH4QPCwgetEwXCWx+UilEBbBZ7mfyrzvjW9kWM6l7vTzyYRsMdTQnqs0Xcwdv7PK4WlQ/lMs/WeNcEfUBu+sWE2zo16kBojNHypOun1sfg2FyLLmvpJOFznG4qUurJN5Lik9zYmRrVAAvwFbShkgk9zsCFcV/FSUf65Vd1BagejWgckj54po1dNQgeDLKJ1IuM8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ItYAM8fMTEwd8bRrl9UO+nwANgMyZyuf4aeMUj7lF44osVT/fUcMmD60cP9+7SoQBFnpiFmzDtPsk9C3oF7Uv6lOui9lrud2+ryQSUsM39+njpXonFKfr1BO";
    let token_bytes = general_purpose::STANDARD.decode(token_str).expect("Failed to decode base64 token");
    let vcca_token = VccaToken::new(&token_bytes).unwrap();
    let json_value = vcca_token.to_json_value();

    assert!(json_value.is_object());
    assert!(json_value.get("vcca_rpv").is_some());
    assert!(json_value.get("vcca_rim").is_some());
    assert!(json_value.get("vcca_rem0").is_some());
    assert!(json_value.get("vcca_cvm_token_hash_alg").is_some());
}